package cn.xiaomubiao.oschine.fragment;

import android.Manifest;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.uuzuche.lib_zxing.activity.CaptureActivity;
import com.uuzuche.lib_zxing.activity.CodeUtils;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;
import cn.xiaomubiao.oschine.R;
import cn.xiaomubiao.oschine.activity.EventActivity;
import cn.xiaomubiao.oschine.activity.FindUserActivity;
import cn.xiaomubiao.oschine.activity.MyInfoBaseActivity;
import cn.xiaomubiao.oschine.activity.ShakeActivity;
import cn.xiaomubiao.oschine.base.BaseNavFragment;
import cn.xiaomubiao.oschine.fragment.Event_fragment.FrientFragment;
import cn.xiaomubiao.oschine.util.PermissionUtils;
import cn.xiaomubiao.oschine.util.UIUtils;

import static cn.xiaomubiao.oschine.R.id.event;
import static cn.xiaomubiao.oschine.R.id.scan;

/**
 * Created by King Su on 2017/4/1.
 * 发现模块
 */

public class ExploreFragment extends BaseNavFragment {

    //二维码的请求常量
    private static final int REQUEST_CODE = 1;
    @BindView(R.id.friends)
    Button mFriends;
    @BindView(R.id.finduser)
    Button mFinduser;
    @BindView(event)
    Button mEvent;
    @BindView(scan)
    Button mScan;
    @BindView(R.id.shake)
    Button mShake;
    @BindView(R.id.activity_main)
    LinearLayout mActivityMain;
    Unbinder unbinder;

    @Override
    public View initView() {
        return View.inflate(UIUtils.getContext(), R.layout.view_explore_fragment, null);
    }

    @Override
    public void initData() {

    }

    @Override
    public View onCreateView( LayoutInflater inflater, @Nullable ViewGroup container,@Nullable Bundle savedInstanceState) {
        // TODO: inflate a fragment view
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        unbinder = ButterKnife.bind(this, rootView);
        return rootView;

    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @OnClick({R.id.friends, R.id.finduser, event, scan, R.id.shake, R.id.activity_main})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.friends:
                //好友圈感觉过于复杂，等到最后来弄；
                Intent intent5 = new Intent(getActivity(), MyInfoBaseActivity.class);
                intent5.putExtra("title","好友圈");
                intent5.putExtra("className", FrientFragment.class.getName());
                startActivity(intent5);
                break;
            case R.id.finduser:
                //找好友
                Intent intent = new Intent(getContext(), FindUserActivity.class);
                startActivity(intent);
                break;
            case event:
                //活动
                Intent intent2 = new Intent(getContext(), EventActivity.class);
                startActivity(intent2);
                break;
            case scan:
                //扫一扫
                /* 打开默认二维码扫描界面
                    */
                PermissionUtils.checkPermission(getActivity(), Manifest.permission.CAMERA);
                Intent intent3 = new Intent(getActivity(), CaptureActivity.class);
                startActivityForResult(intent3, REQUEST_CODE);
                break;
            case R.id.shake:
                Intent intent4 = new Intent(getActivity(), ShakeActivity.class);
                startActivity(intent4);
                break;
            case R.id.activity_main:
                break;
        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQUEST_CODE) {
            //处理扫描结果（在界面上显示）
            if (null != data) {
                Bundle bundle = data.getExtras();
                if (bundle == null) {
                    return;
                }
                if (bundle.getInt(CodeUtils.RESULT_TYPE) == CodeUtils.RESULT_SUCCESS) {
                    String result = bundle.getString(CodeUtils.RESULT_STRING);
                    Toast.makeText(getContext(), "解析结果:" + result, Toast.LENGTH_LONG).show();
                } else if (bundle.getInt(CodeUtils.RESULT_TYPE) == CodeUtils.RESULT_FAILED) {
                    Toast.makeText(getContext(), "解析二维码失败", Toast.LENGTH_LONG).show();
                }
            }
        }
    }
}
